/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.chenmg.Servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.chenmg.DataModel.DataProvider;
/**
 *
 * @author chenmg
 */
public class Login extends HttpServlet{
    private final String SUCCESS_VIEW = "member.view";
    private final String ERROR_VIEW ="login.jsp";
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,IOException{
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        if(checkLogin(userName,userPassword)){
            request.getRequestDispatcher(SUCCESS_VIEW).forward(request,response);
        }else{
            response.sendRedirect(ERROR_VIEW);
        }
    }
    private boolean checkLogin(String username,String password){
        if(username!= null && password != null){
            return !DataProvider.isResultSetNull("SELECT * FROM TEST.USER "
                    +"WHERE USERNAME = '"+ username + "' AND PASSWORD = '"+ password + "'");
        }
        return false;
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException,IOException{
        processRequest(request,response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException{
        processRequest(request,response);
    }
}
